<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class LisenceRepository
{
    /**
     * 获取资质分类列表
     * @param  $page
     * @param  $perPage
     * @return bool
     */
    public function typeList($page=1,$perPage=20)
    {
       $typeList =  app('model_lisence')
           ->where('pid',0)
           ->where('status','<>',3)
           ->orderBy('sort','desc')
           ->select('lisence_id','lisence_name','icon','sort','pid','level','add_time','status')
           ->paginate($perPage, ['*'], '', $page);
       return $typeList;
    }




    /**
     * 获取资质列表
     * @param  $param
     * @param  $page
     * @param  $perPage
     * @return bool
     */
    public function list($param,$page=1,$perPage=20)
    {
        $typeList =  app('model_lisence')
            ->where('pid','>',0)
            ->where('status','<>',3)
            ->orderBy('sort','desc');
            if (isset($param['status'])){
                $typeList=$typeList->where('status',$param['status']);
            }
            if (isset($param['lisence_name'])){
                $typeList=$typeList->where('lisence_name','like',"%{$param['lisence_name']}%");
            }
        $typeList =$typeList ->paginate($perPage, ['*'], '', $page);
        return $typeList;
    }

    /**
     * 新增资质分类
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public  function  addType($param){
        $add = app('model_lisence')->create($param);
        if ($add) return $add;
        throw new BasicException(93115);
    }


    /**
     * 新增资质
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public  function  add($param){
        $add = app('model_lisence')->create($param);
        if ($add) return $add;
        throw new BasicException(93119);
    }

    /**
     * 编辑资质分类
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public  function  saveType($param){
        $save = app('model_lisence')->where('lisence_id',$param['lisence_id'])->update($param);
        if ($save!==false) return true;
        throw new BasicException(93116);
    }



    /**
     * 编辑资质
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public  function  save($param){
        $save = app('model_lisence')->where('lisence_id',$param['lisence_id'])->update($param);
//        if ($save) return $save;
//        throw new BasicException(93120);
    }

    /**
     * 用于新增、编辑的资质分类列表
     * @return bool
     */
    public  function operateList(){
        $list = app('model_lisence')->where([['pid',0],['status','<',3]])->select('lisence_id','lisence_name','icon')->orderBy('sort','desc')->get()->toArray();
        return $list;
    }

    /**
     * 删除资质分类
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function  delType($param){
        $del = app('model_lisence')->whereIn('lisence_id',$param)->update(['status'=>3]);
        if ($del) return $del;
        throw new BasicException(93118);
    }


    /**
     * 删除资质
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function  delLisence($param){
        $del = app('model_lisence')->whereIn('lisence_id',$param)->update(['status'=>3]);
        if ($del) return $del;
        throw new BasicException(93121);
    }

    /**
     * 获取资质详情
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function detail($id){
        $detail =app('model_lisence')->where('lisence_id',$id)->first();
        if ($detail) return $detail;
        throw new BasicException(93117);
    }

    /**
     * 暂停资质
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function suspend($id){
        $suspend =app('model_lisence')->where('lisence_id',$id)->update(['status'=>2]);
        if ($suspend) return $suspend;
        throw new BasicException(93122);
    }


    /**
     * 开启资质
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function open($id){
        $suspend =app('model_lisence')->where('lisence_id',$id)->update(['status'=>1]);
        if ($suspend) return $suspend;
        throw new BasicException(93140);
    }

    /**
     * 资质列表用来搜索
     * @return bool
     */

    public  function  lisenceSearchList(){
        $list =app('model_lisence')->where('status','<>',3)->where('pid','<>',0)->select('lisence_id','lisence_name')->get()->toArray();
        return $list;
    }

    /**
     * 获取资质详情
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public  function  lisenceDetail($id){
        $detail = app('model_lisence')->where('lisence_id',$id)->first();
        if ($detail)  return $detail;
        throw  new  BasicException(50019);
    }

}


?>